<?php

namespace Addons\CustomReply\Controller;

use Addons\CustomReply\Controller\BaseController;

class CustomReplyController extends BaseController
{
    var $model;

    function _initialize()
    {
        $this->model = $this->getModel('custom_reply_news');
        parent::_initialize();
    }

    // 通用插件的列表模型
    public function lists()
    {
        $map ['token'] = get_token();
        session('common_condition', $map);

        $list_data = $this->_get_model_list($this->model);

        // 分类数据
        $map ['is_show'] = 1;
        $list = M('weisite_category')->where($map)->field('id,title')->select();
        $cate [0] = '';
        foreach ($list as $vo) {
            $cate [$vo ['id']] = $vo ['title'];
        }

        foreach ($list_data ['list_data'] as &$vo) {
            $vo ['cate_id'] = intval($vo ['cate_id']);
            $vo ['cate_id'] = $cate [$vo ['cate_id']];
        }
        $this->assign($list_data);
        // dump ( $list_data );

        $templateFile = $this->model ['template_list'] ? $this->model ['template_list'] : '';
        $this->display($templateFile);
    }

    // 通用插件的编辑模型
    public function edit()
    {
        $model = $this->model;
        $id = I('id');

        if (IS_POST) {
            $Model = D(parse_name(get_table_name($model ['id']), 1));
            // 获取模型的字段信息
            $Model = $this->checkAttr($Model, $model ['id']);
            if ($Model->create() && $Model->save()) {
                $this->_saveKeyword($model, $id, 'custom_reply_news');

                // 清空缓存
                method_exists($Model, 'clear') && $Model->clear($id, 'edit');

                $this->success('保存' . $model ['title'] . '成功！', U('lists?model=' . $model ['name']));
            } else {
                $this->error($Model->getError());
            }
        } else {
            $fields = get_model_attribute($model ['id']);

            $extra = $this->getCateData();
            if (!empty ($extra)) {
                foreach ($fields as &$vo) {
                    if ($vo ['name'] == 'cate_id') {
                        $vo ['extra'] .= "\r\n" . $extra;
                    }
                }
            }

            // 获取数据
            $data = M(get_table_name($model ['id']))->find($id);
            $data || $this->error('数据不存在！');

            $token = get_token();
            if (isset ($data ['token']) && $token != $data ['token'] && defined('ADDON_PUBLIC_PATH')) {
                $this->error('非法访问！');
            }

            $this->assign('fields', $fields);
            $this->assign('data', $data);
            $this->meta_title = '编辑' . $model ['title'];

            $this->display();
        }
    }

    // 通用插件的增加模型
    public function add()
    {
        $model = $this->model;
        $Model = D(parse_name(get_table_name($model ['id']), 1));

        if (IS_POST) {
            // 获取模型的字段信息
            $Model = $this->checkAttr($Model, $model ['id']);
            if ($Model->create() && $id = $Model->add()) {
                $this->_saveKeyword($model, $id, 'custom_reply_news');

                // 清空缓存
                method_exists($Model, 'clear') && $Model->clear($id, 'edit');

                $this->success('添加' . $model ['title'] . '成功！', U('lists?model=' . $model ['name']));
            } else {
                $this->error($Model->getError());
            }
        } else {
            $fields = get_model_attribute($model ['id']);

            $extra = $this->getCateData();
            if (!empty ($extra)) {
                foreach ($fields as &$vo) {
                    if ($vo ['name'] == 'cate_id') {
                        $vo ['extra'] .= "\r\n" . $extra;
                    }
                }
            }

            $this->assign('fields', $fields);
            $this->meta_title = '新增' . $model ['title'];

            $this->display();
        }
    }

    // 通用插件的删除模型
    public function del()
    {
        parent::common_del($this->model);
    }

    // 获取所属分类
    function getCateData()
    {
        $map ['is_show'] = 1;
        $map ['token'] = get_token();
        $list = M('weisite_category')->where($map)->select();
        foreach ($list as $v) {
            $extra .= $v ['id'] . ':' . $v ['title'] . "\r\n";
        }
        return $extra;
    }

    // 内容页面
    function detail()
    {
        $id = I('get.id', 0, 'intval');

        $info = D('Addons://CustomReply/CustomReply')->getInfo($id);
        $this->assign('info', $info);

        D('Common/Count')->set('custom_reply_news', $id, 'view_count');

        // 增加积分
        add_credit('custom_reply', 300);

        $this->display();
    }
}
